<?php

namespace app\model\store;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

class StoreBannerModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'store_banner';

    /**
     * 获取器-$status
     *
     * @param [type] $value
     * @return void
     */
    public function getStatusAttr($value)
    {
        $arr = ['0' => '下线', '1' => '上线'];
        return $arr[$value];
    }

    /**
     * 范围查询-store_id
     *
     * @param [type] $query
     * @param [type] $store_id
     * @return void
     */
    public function scopeStore($query, $store_id)
    {
        if (!empty($store_id)) {
            $query->where('store_id', $store_id);
        }
    }

    /**
     * 范围查询-keyword
     *
     * @param [type] $query
     * @param [type] $keyword
     * @return void
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->where('keyword', $keyword);
        }
    }

    /**
     * 范围查询-status
     *
     * @param [type] $query
     * @param [type] $status
     * @return void
     */
    public function scopeStatus($query, $status)
    {
        if (!empty($status)) {
            $query->where('status', $status);
        }
    }

    /**
     * 分页查询
     *
     * @param [type] $keyword
     * @param [type] $status
     * @param [type] $store_id
     * @param [type] $current_page
     * @param [type] $list_rows
     * @return void
     */
    public function getPageData($keyword, $status, $store_id, $current_page, $list_rows)
    {
        $dataList = $this
            ->keyword($keyword)
            ->store($store_id)
            ->status($status)
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $items = $dataList->items();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $items];
        return $ret;
    }

    /**
     * 获取全部
     *
     * @param [type] $limit
     * @param [type] $store_id
     * @return void
     */
    public function getAllData($limit, $store_id)
    {
        $data = $this
            ->store($store_id)
            ->status(1)
            ->limit($limit)
            ->order('rank desc')
            ->select();
        return $data;
    }
}